<?php
// +----------------------------------------------------------------------
// | QbtCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 千佰特 <http://www.qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_payment_management\controller;

use cmf\controller\PluginAdminBaseController;
use think\Db;
class AdminIndexController extends PluginAdminBaseController
{
    /**
     * @adminLog(
     *     'public/plugin/qbt_payment_management/admin_index/edit|修改支付配置ID:{id}',
     * )
     */
    public function index()
    {
        $wx =  Db::name('payment_management')->where('id=1')->find();
        $ali =  Db::name('payment_management')->where('id=2')->find();
        $this->assign("wx", json_decode($wx['config_json'],true));
        $this->assign("ali", json_decode($ali['config_json'],true));
        return $this->fetch('/admin_index');
    }

    public function index1()
    {
        $list =  Db::name('payment_log')->paginate(10);
        $this->assign("list", $list);
        $this->assign('page',$list->render());
        return $this->fetch('/admin_index1');
    }

    //修改
    public function edit(){
        $id = $this->request->param('id');
        if($_POST){
            //微信
            if ($id==1){
                $data1['appid'] = $this->request->param('appid');
                $data1['mchid'] = $this->request->param('mchid');
                $data1['key'] = $this->request->param('key');
                $data1['appsecret'] = $this->request->param('appsecret');
                $data1['pay_name'] = $this->request->param('pay_name');
            }
            //支付宝
            if ($id==2){
                $data1['pay_name'] = $this->request->param('pay_name');
                $data1['appid'] = $this->request->param('appid');
                $data1['merchant_private_key'] = $this->request->param('merchant_private_key');
                $data1['alipay_public_key'] = $this->request->param('alipay_public_key');
                $data1['sign_type'] = $this->request->param('sign_type');
            }
            $data['config_json'] = json_encode($data1);
            $result =  Db::name('payment_management')->where("id = $id")->update($data);
            if ($result){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }else{
            $info =  Db::name('payment_management')->where("id = $id")->find();
            $this->assign("info", json_decode($info['config_json'],true));
            $this->assign("id", $id);
            return $this->fetch('/edit');
        }
    }
}